<?php

$param = file_get_contents('php://input');
// $param = file_get_contents('notify1.txt');
file_put_contents('pay_refund.json', $param);
$param = json_decode($param, true);
$url = file_get_contents('notify_url.txt');
if (!empty($param['msg'])) {

    $msg = $param['msg'];
    $msg = json_decode($msg, true);
    if ($msg['status'] == "SUCCESS") {
        send_post($url, arrayToXml([
            "out_order_no" => $msg['out_order_no'],
            "status" => "SUCCESS2",
            // "status" => $msg['status'],
            "order_id" => $msg['order_id'],
            "total_amount" => $msg['total_amount'],
            "way" => $msg['pay_channel'],
            "cp_extra" => json_decode($msg['cp_extra'], true)['order_id']
        ]));
        echo json_encode([
            'err_no' => 0,
            'err_tips' => 'success',

        ]);
    } else {
        echo json_encode([
            'err_no' => 1,
            'err_tips' => 'system error',

        ]);
    }

    // var_dump($msg['goods'][0]['goods_id']);
    // exit;

} else {
    //
    // echo json_encode([
    //     'err_no' => 1,
    //     'err_tips' => 'success',

    // ]);
}



exit;
function arrayToXml($arr)
{

    $xml = "<xml>";
    foreach ($arr as $key => $val) {
        if (is_numeric($val)) {
            $xml .= "<$key>$val</$key>";
        } else
            $xml .= "<$key><![CDATA[$val]]></$key>";
    }
    $xml .= "</xml>";
    return $xml;
}
function send_post($url, $post_data)
{
    // $postdata = http_build_query($post_data);
    file_put_contents('xml.txt', $post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/xml',
            'content' => $post_data,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}
